home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18564 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.ada,comp.lang.c,comp.lang.c++,comp.edu
  4. Subject: Re: ANSI C and POSIX (was Re: C/C++ knocks the crap out of Ada)
  5. Date: Sun, 21 Apr 1996 13:35:04 GMT
  6. Organization: Netcom
  7. Message-ID: <317a3715.38162294@nntp.ix.netcom.com>
  8. References: <4kk9e1$he1@nntp.Stanford.EDU> <dewar.829276268@schonberg> <01bb2ed2.425fb900$65c2b7c7@Zany.localhost>
  9. NNTP-Posting-Host: ix-dc19-08.ix.netcom.com
  10. X-NETCOM-Date: Sun Apr 21  8:35:54 AM CDT 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Bradd W. Szonye <bradds@ix.netcom.com> wrote:
  14.  
  15. > Try to keep in mind the spirit of defensive programming:
  16. > If there's something ambiguous about the way you could implement
  17. > something, and one implementation is safe regardless of how you interpret
  18. > the ambiguity, the other implementation only works under one specific
  19. > interpretation, then defensive programming (and portable programming) will
  20. > encourage the code that works under all circumstances. Consider:
  21. > for (size_t i = 0; i < 10; i++) do_stuff();
  22. > versus
  23. > for (size_t i = 0; i != 10; i++) do_stuff();
  24. > Even though you *know* that i will never be greater than 10, even though
  25. > "not equals" should always stop the loop after the tenth iteration,
  26. > practically every programmer will write the first loop in preference to
  27. > the second. This has nothing to do with standards; the standards say that
  28. > i is a local, stack-based variable, not global, and since it is not
  29. > volatile or referenced by anything else, do_stuff() couldn't modify it,
  30. > even another thread couldn't modify it. But should your memory chips fail,
  31. > or do_stuff() accidentally trash the stack with a pointer, then the first
  32. > loop will never let i get out of the range of 0 <= i < 10, while the
  33. > second loop might.
  34.  
  35. What nonsense.  Most programmers write the loop the first way out of
  36. habit and for consistency.  Suppose we change the loop a little:
  37.  
  38.     for (size_t i = a; i < 10; i++) do_stuff();
  39.  
  40. versus
  41.  
  42.     for (size_t i = a; i != 10; i++) do_stuff();
  43.  
  44. Now the loops mean different things.  Either might be correct, but the
  45. first is by far the more common.
  46.  
  47. If the stack is trashed or the memory chips have failed, why do you
  48. want to get out of the loop?  Why do you assume that getting out of
  49. the loop prematurely is better than not getting out of it at all?
  50. Suppose the loop is part of a sort.  If you stay in it, the program
  51. will hang and the user will complain.  If you exit it the program may
  52. continue and output erroneous information that causes costly errors.
  53.  
  54. Defensive programming is important, but coding so you'll exit a loop
  55. even if the hardware fails has nothing to do with it.
  56.  
  57.  
  58. Michael M Rubenstein
  59.